let that = null;
let mine = require("../../../api/mine.js")
Page({
    data: {
        bindtap: 'login',
        to: ''
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function (options) {
        that = this;
        that.setData({
            to: options.to
        })
    },
    /*
      **登录**
      */
    async login() {
        that.setData({
            bindtap: ''
        })
        await wx.getUserProfile({
            desc: '作为用户信息使用',
            async success(res) {
                wx.showLoading({
                    title: '加载中...',
                    mask: true,
                })
                console.log(res.userInfo);
                const login = await mine.login().then(res => { return res; });
                //用户是否存在
                wx.hideLoading({
                    success: (res) => { console.log('数据加载完成') },
                })
                if (login.login == 0) {
                    wx.setStorageSync('user', res.userInfo)
                    wx.redirectTo({
                        url: '../Info/Info?to=' + that.data.to,
                    })
                } else {
                    await wx.setStorageSync('user', login.data);
                    //判断是否是点击维修按钮跳转到登录
                    if (that.data.to == '/miniprogram/pages/CA/useRepair/userRepair') {
                        // 判断是否是维修者
                        if (login.data.is_repair == 1) {
                            wx.redirectTo({
                                url: '../../CA/Repair/Repair',
                            })
                        } else {
                            const user = wx.getStorageSync('user');
                            // 判断是否填写完整信息
                            if (user.is_repair != undefined) {
                                wx.redirectTo({
                                    url: '../../CA/useRepair/userRepair',
                                })
                            } else {
                                wx.redirectTo({
                                    url: '../../mine/Info/Info?to=/miniprogram/pages/CA/useRepair/userRepair',
                                })
                            }
                        }
                    } else if (that.data.to == '/miniprogram/pages/CA/History/History') {
                        // 判断是否为维修者
                        if (login.data.is_repair == 1) {
                            wx.redirectTo({
                                url: '../../CA/Repair/Repair?to=history',
                            })
                        } else {
                            wx.redirectTo({
                                url: '../../CA/History/History',
                            })
                        }
                    } else if (that.data.to == '/miniprogram/pages/mine/Info/Info') {
                        wx.redirectTo({
                            url: '../Info/Info',
                        })
                    } else {
                        wx.redirectTo({
                          url: that.data.to,
                        })
                    }
                }
                console.log('微信登陆成功！')
            },
            fail: (res) => {
                console.log('登录失败！');
                that.setData({
                    bindtap: 'login',
                })
            }
        })
    },
    
    /**
     * 右上角分享
     */
    onShareAppMessage() {
        return {
            title: '首页',
            path: '/pages/CA/home/home',
            imageUrl: '../../../images/share.png'
        }
    },
})